package oa.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 工具类，生成图片验证码
 */
public class SecurityImage {

	private static String[] fontNames = {"宋体","黑体","微软雅黑","华文楷体","楷体_GB2312"};
	private static Random random = new Random();
	
	public static BufferedImage createImage(String securityCode)
	{
		//获取验证码的长度
		int codeLength = securityCode.length();
		//设置标准字体大小
		int size = 24;
		int fwidth = size+1;
		//设置图片宽度
		int width = codeLength * fwidth;
		//图片高度
		int height = size +15;
		
		//图片
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D)image.createGraphics();
		
		g.setColor(Color.WHITE);//设置背景色
		g.fillRect(0,0,width, height);
		
		g.setColor(Color.GRAY);//设置边框颜色
		g.drawRect(0, 0, width-1, height-1); //绘制边框
		
		//绘制噪点
		Color color = randomColor();
	    g.setColor(color);//设置噪点颜色
	    for (int i = 0; i < codeLength*6; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			//绘制1*1大小的矩形
			g.drawRect(x, y, 1, 1);
		}
	    
	    //绘制验证码
	    int codeY = height -10;
	    for (int i = 0; i < codeLength; i++) {
	    	String s = securityCode.charAt(i)+"";
	    	int findex = random.nextInt(fontNames.length);
	    	String fontName = fontNames[findex];
			g.setFont(new Font(fontName, random.nextInt(4), random.nextInt(5)+size));
		    color = randomColor();
		    g.setColor(color); //设置字体颜色
		    g.drawString(s, i*size + 5, codeY);
		}
	    int num = 3;
		for(int i=0;i<num;i++)
		{
			int x1 = random.nextInt(width);
			int y1 = random.nextInt(height);
			int x2 = random.nextInt(width);
			int y2 = random.nextInt(height);
			g.drawLine(x1, y1, x2, y2);
		}
	    //关闭资源
		g.dispose();
	
		return image;
	}
    
	/**
	 * 返回验证码图片的流格式
	 * @param securityCode
	 * @return
	 */
	 public static  ByteArrayInputStream getImageAsInputStream(String securityCode){
		  BufferedImage image = createImage(securityCode);
		  return convertImageToStream(image);
	 }
	 
	 /**
	  * 将BufferedImage转换成ByteArrayInputStream
	  * @param image
	  * @return
	  */
	public static ByteArrayInputStream convertImageToStream(BufferedImage image) {
		// TODO Auto-generated method stub
		ByteArrayInputStream inputStream = null;
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(outputStream);
		
		try {
			jpeg.encode(image);
			byte[] bytes=outputStream.toByteArray();
			inputStream = new ByteArrayInputStream(bytes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return inputStream;
	}

	//产生随机色
	private  static Color randomColor() {
		int red = random.nextInt(150);
		int green = random.nextInt(150);
		int blue = random.nextInt(150);	
		Color color = new Color(red, green, blue);
		return color;
	}
}
